/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.upms.biz.userLog.service.impl;

import co.yixiang.common.core.utils.FileUtil;
import co.yixiang.common.mproot.common.service.impl.BaseServiceImpl;
import co.yixiang.common.mproot.common.utils.QueryHelpPlus;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.upms.biz.userLog.service.YxUserLogService;
import co.yixiang.upms.biz.userLog.service.mapper.YxUserLogMapper;
import co.yixiang.upms.common.userLog.domain.YxUserLog;
import co.yixiang.upms.common.userLog.dto.YxUserLogDto;
import co.yixiang.upms.common.userLog.dto.YxUserLogQueryCriteria;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @author guoqingming
* @date 2021-03-25
*/
@Service
//@CacheConfig(cacheNames = "yxUserLog")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxUserLogServiceImpl extends BaseServiceImpl<YxUserLogMapper, YxUserLog> implements YxUserLogService {

    private final IGenerator generator;

    public YxUserLogServiceImpl(IGenerator generator) {
        this.generator = generator;
    }

    @Override
    //@Cacheable
    public PageResult<YxUserLogDto> queryAll(YxUserLogQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxUserLog> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,YxUserLogDto.class);
    }


    @Override
    //@Cacheable
    public List<YxUserLog> queryAll(YxUserLogQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxUserLog.class, criteria));
    }


    @Override
    public void download(List<YxUserLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxUserLogDto yxUserLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", yxUserLog.getCreateTime());
            map.put("修改人", yxUserLog.getModifyUser());
            map.put("请求ip", yxUserLog.getRequestIp());
            map.put("原数据", yxUserLog.getOriginalData());
            map.put("新数据", yxUserLog.getNewData());
            map.put("更新时间", yxUserLog.getUpdateTime());
            map.put("会员id", yxUserLog.getUid());
            map.put("字段标识", yxUserLog.getFieldLogo());
            map.put("修改原因", yxUserLog.getModifyReason());
            map.put("是否删除", yxUserLog.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
